﻿<?php
/*
项目名称：在线简历填写+公众号简历状态实时推送查询系统
微信消息接口开发：@刘继鹏Beta
程序开发：@刘继鹏Beta
前端设计:@刘继鹏Beta
MYSQL：@刘继鹏Beta
开发日期：2017/02/13
*/
error_reporting(0);
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
    $wechatObj->valid();
}else{
    $wechatObj->responseMsg();
}

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            header('content-type:text');
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUsername = $postObj->FromUserName;
            $toUsername = $postObj->ToUserName;
            $keyword = trim($postObj->Content);
            $time = time();
			$ev = $postObj->Event;
           
$con = mysql_connect ("localhost","root","root");
            @mysql_select_db("jianli", $con); 
            mysql_query("SET NAMES UTF8");
 				$result = mysql_query("SELECT * FROM LJ_data WHERE opid  LIKE '$fromUsername'  limit 0,1 ");
            while($row = mysql_fetch_array($result)) 	{
	        $sqlzhuangtai=$row['zhuangtai'];
				$sqlxm=$row['xm'];
} 
//关注的回复
if ($ev == "subscribe"){
	$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>2</ArticleCount>
<Articles>
<item>
<Title><![CDATA[欢迎关注xx集团微信公众平台]]></Title> 
<Description><![CDATA[]]></Description>
<PicUrl><![CDATA[]]></PicUrl>
<Url><![CDATA[]]></Url>
</item>
<item>
<Title><![CDATA[xx集团微信平台支持以下功能\r\n
【1】直接回复‘简历’在线填写面试简历\r\n
【2】直接回复‘查询’获取简历审核状态\r\n
【3】更多优秀功能正在快马加鞭研发中
]]></Title>
<Description><![CDATA[]]></Description>
<PicUrl><![CDATA[]]></PicUrl>
<Url><![CDATA[]]></Url>
</item>
</Articles>
</xml> ";
  $msgType = "text";
  $contentStr = "欢迎关注！";
  $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  echo $resultStr;	
}
//没有找到关键词的回复
if($num==0){$num="1";}
if($sda==""){$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>2</ArticleCount>
<Articles>
<item>
<Title><![CDATA[哎呀！xx没有找到你输入的关键字]]></Title> 
<Description><![CDATA[]]></Description>
<PicUrl><![CDATA[]]></PicUrl>
<Url><![CDATA[]]></Url>
</item>
<item>
<Title><![CDATA[xx集团微信平台支持以下功能\r\n
【1】直接回复‘简历’在线填写面试简历\r\n
【2】直接回复‘查询’获取简历审核状态\r\n
【3】更多优秀功能正在快马加鞭研发中
]]></Title>
<Description><![CDATA[]]></Description>
<PicUrl><![CDATA[]]></PicUrl>
<Url><![CDATA[]]></Url>
</item>
</Articles>
</xml>";
}
            if(!empty( $keyword )) 
            {
			$msgType = "news";
			                switch ($keyword)
{

		case "简历";
					
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime><![CDATA[%s]]></CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[<a href='你的网站/index.php?opid=$fromUsername'>点我在线填写简历</a>]]></Content>
</xml>";
		break;
				case "查询";

	if($sqlzhuangtai=='审核中'){
		$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[简历状态查询结果]]></Title> 
<Description><![CDATA[亲爱的".$sqlxm."\r\n\r\n以下是你的简历状态\r\n
审核状态：审核中...\r\n
亲！你的简历正在人事MM处快马加鞭的审核中\r\n
无论审核通过与否我们都会在公众号内回复你\r\n
还请不要取消关注我们的公众号哟，谢谢合作]]></Description>
<PicUrl><![]]></PicUrl>
<Url><![CDATA[]]></Url>
</item>
</Articles>
</xml>";
	}elseif($sqlzhuangtai=='通过'){
		$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[简历状态查询结果]]></Title> 
<Description><![CDATA[亲爱的".$sqlxm."\r\n\r\n以下是你的简历状态\r\n
审核状态：通过\r\n\n恭喜你已成功加入xx集团\n\n以下为注意事项\n\n以下为入职须知\n]]></Description>
<PicUrl><![CDATA[]]></PicUrl>
<Url><![CDATA[]]></Url>
</item>
</Articles>
</xml>";
	}elseif($sqlzhuangtai=='未通过'){
		$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[简历状态查询结果]]></Title> 
<Description><![CDATA[亲爱的".$sqlxm."\r\n\r\n以下是你的简历状态\r\n
审核状态：未通过\r\n\n很遗憾你没有通过本次面试\n\n祝你早日找到理想工作\n]]></Description>
<PicUrl><![CDATA[]]></PicUrl>
<Url><![CDATA[]]></Url>
</item>
</Articles>
</xml>";
	}else{
		$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[你还没有提交简历哦！]]></Title> 
<Description><![CDATA[]]></Description>
<PicUrl><![CDATA[]]></PicUrl>
<Url><![CDATA[]]></Url>
</item>
</Articles>
</xml>"; 
	}break;

}
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            echo $resultStr;
            }
			
        }else {

	echo "";

            exit;
        }
    }
	
}
    
?>